/******************************************************************************
 *                       B O M B E R M A N  2 0 1 2
 * 
 * File Name: ServerSocket.java
 * 
 * COP 4331 Summer 2012 Project
 * 
 * Authors: Trevor Mancuso, Chris Chiarella, Zach Hoyler, Daniel Martin
 * 
 * 
 * Description: Socket connection for the server.
 * 
 ******************************************************************************/
package bomberman;

import java.io.IOException;
import java.net.*;
import java.util.logging.Level;
import java.util.logging.Logger;

public class ServerSocket extends GameSocket implements Runnable  {
    
    DatagramSocket serverSocket;
    DatagramSocket serverSendingSocket;
    boolean running = true;
    
    Bomberman game;
  
    byte[] receiveData = new byte[1024]; 
    byte[] sendData  = new byte[1024];
    //Constructor
    ServerSocket(Bomberman game) throws IOException
    {
        serverSocket= new DatagramSocket(4321);
        serverSendingSocket = new DatagramSocket();
        this.game = game;                
    }
    //When in server mode, listen on port 4321 and send on port 4320 to clients.
    public void run()
    {
        
        while(running)
        {
            try {
                listen();
            } catch (IOException ex) {
                Logger.getLogger(ServerSocket.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        
        return;
    }
    //listen for a new packet, when recevied, pass it to the NetworkHandler
    public void listen() throws IOException
    {
          receiveData = new byte[1024]; 

          DatagramPacket receivePacket = 
             new DatagramPacket(receiveData, receiveData.length); 

          System.out.println ("Waiting for datagram packet");

          try{
          serverSocket.receive(receivePacket); 
          }catch(SocketException ex)
          {
              stopRunning();
              closeSockets();
          }

          String sentence = new String(receivePacket.getData()); 
  
          InetAddress IPAddress = receivePacket.getAddress(); 
  
          int port = receivePacket.getPort(); 
          
          game.handler.handleMessage(sentence);
  
          System.out.println ("From: " + IPAddress + ":" + port);
          System.out.println ("Message: " + sentence);

//          String capitalizedSentence = sentence.toUpperCase(); 
//
//          sendData = capitalizedSentence.getBytes(); 
//  
//          DatagramPacket sendPacket = 
//             new DatagramPacket(sendData, sendData.length, IPAddress, 
//                               port); 
//  
//          serverSocket.send(sendPacket);               
            
        
    }
    //Send a new message
    public void sendMessage(String message,String address) throws UnknownHostException, IOException
    {
        byte[] sendData = new byte[1024]; 
        
        sendData = message.getBytes();
        
        System.out.println ("SERVER SENDING: Sending data to " + sendData.length + 
                          " bytes to server.");
        
        DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length,InetAddress.getByName(address),4320);
        
        serverSendingSocket.send(sendPacket);
    }
    //close the socket
    public void closeSockets()
    {
        System.out.println("Closing server sockets");
        serverSocket.close();
        serverSendingSocket.close();
    }
    //stop the listening thread
    public void stopRunning()
    {
        running = false;
    }
    
}
